﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LCUV;
using Telerik.Web.UI;

namespace sWarranty.WEB
{
    public partial class ProductPeriodINQ : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "ProductPeriodINQ.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {
                //BuildTable(dropYear.SelectedValue, "%", "%", "%");
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                   
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }

        protected void BuildTable(string serial, string model)
        {
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_PROD_WT_PER_INQ", "uiroi", "P_SERIAL_NO", serial, "P_MODEL", model, 
                null, null, null, null, null, null, null, null, null, null, null, null, null, null
                , null, null, null, null, null, null
                , null, null, null, null, null, null);//from.ToString("yyyyMMdd"), to.ToString("yyyyMMdd"), _BillCode, _product, _model));
            int dateTOShow = Convert.ToInt32(DateTime.Now.DayOfWeek); //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            string s = null;
            string m = "%";
            if (!string.IsNullOrEmpty(txtSerial.Text.Trim()))
                s = txtSerial.Text.Trim().ToUpper();
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                m = txtModel.Text.Trim().ToUpper();

            BuildTable(s, m );

        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "var btn=document.getElementById('btnFind');btn.click();", true);
            string s = null;
            string m = "%";
            if (!string.IsNullOrEmpty(txtSerial.Text.Trim()))
                s = txtSerial.Text.Trim().ToUpper();
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                m = txtModel.Text.Trim().ToUpper();

            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_PROD_WT_PER_INQ", "uiroi", "P_SERIAL_NO", s, "P_MODEL", m,
                null, null, null, null, null, null, null, null, null, null, null, null, null, null
                , null, null, null, null, null, null
                , null, null, null, null, null, null);//from.ToString("yyyyMMdd"), to.ToString("yyyyMMdd"), _BillCode, _product, _model));
            int dateTOShow = Convert.ToInt32(DateTime.Now.DayOfWeek); //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                if (e.Action == GridGroupsChangingAction.Ungroup)
                    RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }

    }
}